{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5.2\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "from paddle.nn import Linear\n",
    "import numpy as np\n",
    "import os\n",
    "import json\n",
    "import random\n",
    "print(paddle.__version__)\n",
    "from paddle.nn import Conv2D,MaxPool2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('mnist.json')as f:\n",
    "        data=json.load(f)\n",
    "    train_set,val_set,eval_set=data\n",
    "    if mode=='train':\n",
    "        imgs,labels=train_set[0],train_set[1]\n",
    "    elif mode=='valid':\n",
    "        imgs,labels=val_set[0],val_set[1]\n",
    "    elif mode=='eval':\n",
    "        imgs,labels=eval_set[0],eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train','valid','eval']\")\n",
    "    print(\"训练数据集数量:\",len(imgs))\n",
    "    imgs_length=len(imgs)\n",
    "    index_list=list(range(imgs_length))\n",
    "    BATCHSIZE=100\n",
    "    def data_generator():\n",
    "        if mode=='train':\n",
    "            random.shuffle(index_list)\n",
    "        imgs_list=[]\n",
    "        labels_list=[]\n",
    "        for i in index_list:\n",
    "            img=np.array(imgs[i]).astype('float32')\n",
    "            label=np.reshape(labels[i],[1]).astype('int64')\n",
    "            imgs_list.append(img)\n",
    "            labels_list.append(label)\n",
    "            if len(img_list)==BATCHSIZE:\n",
    "                yield np.array(imgs_list),np.array(label_list)\n",
    "                imgs_list=[]\n",
    "                labels_list=[]\n",
    "        if len(imgs_list)>0:\n",
    "            yield np.array(imgs_list),np.array(labels_list)\n",
    "    return data_generator\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('mnist.json')as f:\n",
    "        data=json.load(f)\n",
    "    train_set,val_set,eval_set=data\n",
    "    if mode=='train':\n",
    "        imgs,labels=train_set[0],train_set[1]\n",
    "    elif mode=='valid':\n",
    "        imgs,labels=val_set[0],val_set[1]\n",
    "    elif mode=='eval':\n",
    "        imgs,labels=eval_set[0],eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train','valid','eval']\")\n",
    "    print(\"训练数据集数量:\",len(imgs))\n",
    "    imgs_length=len(imgs)\n",
    "    index_list=list(range(imgs_length))\n",
    "    BATCHSIZE=100\n",
    "    def data_generator():\n",
    "        if mode=='train':\n",
    "            random.shuffle(index_list)\n",
    "        imgs_list=[]\n",
    "        labels_list=[]\n",
    "        for i in index_list:\n",
    "            img=np.reshape(imgs[i],[1,28,28]).astype('float32')\n",
    "            label=np.reshape(labels[i],[1]).astype('int64')\n",
    "            imgs_list.append(img)\n",
    "            labels_list.append(label)\n",
    "            if len(imgs_list)==BATCHSIZE:\n",
    "                yield np.array(imgs_list),np.array(labels_list)\n",
    "                imgs_list=[]\n",
    "                labels_list=[]\n",
    "        if len(imgs_list)>0:\n",
    "            yield np.array(imgs_list),np.array(labels_list)\n",
    "    return data_generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LeNetModel(paddle.nn.Layer):\n",
    "    def __init__(self):\n",
    "        super(LeNetModel,self).__init__()\n",
    "        self.conv1=paddle.nn.Conv2D(in_channels=1,out_channels=6,kernel_size=5,stride=1)\n",
    "        self.pool1=paddle.nn.MaxPool2D(kernel_size=2,stride=2)\n",
    "        self.conv2=paddle.nn.Conv2D(in_channels=6,out_channels=16,kernel_size=5,stride=1)\n",
    "        self.pool2=paddle.nn.MaxPool2D(kernel_size=2,stride=2) \n",
    "        self.fc1=paddle.nn.Linear(256,120)\n",
    "        self.fc2=paddle.nn.Linear(120,84)\n",
    "        self.fc3=paddle.nn.Linear(84,10)\n",
    "    def forward(self,x):\n",
    "        x=self.conv1(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.pool1(x)\n",
    "        x=self.conv2(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.pool2(x)\n",
    "        x=paddle.flatten(x,start_axis=1,stop_axis=-1)\n",
    "        x=self.fc1(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.fc2(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.fc3(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(model):\n",
    "    model.train()\n",
    "    opt=paddle.optimizer.SGD(learning_rate=0.01,parameters=model.parameters())\n",
    "    EPOCH_NUM=5\n",
    "    for epoch_id in range(EPOCH_NUM):\n",
    "        for batch_id,data in enumerate(train_loader()):\n",
    "            images,labels=data\n",
    "            images=paddle.to_tensor(images)\n",
    "            labels=paddle.to_tensor(labels)\n",
    "            predicts=model(images)\n",
    "            loss=F.softmax_with_cross_entropy(predicts,labels)\n",
    "            avg_loss=paddle.mean(loss)\n",
    "            if batch_id %200 ==0:\n",
    "                print(\"epoch:{},batch:{},loss is:{}\".format(epoch_id,batch_id,avg_loss.numpy()))\n",
    "            avg_loss.backward()\n",
    "            opt.step()\n",
    "            opt.clear_grad()\n",
    "        model.eval()\n",
    "        accuracies=[]\n",
    "        losses=[]\n",
    "        for batch_id,data in enumerate(valid_loader()):\n",
    "            images,labels=data\n",
    "            images=paddle.to_tensor(images)\n",
    "            labels=paddle.to_tensor(labels)\n",
    "            logits=model(images)\n",
    "            pred=F.softmax(logits)\n",
    "            loss=F.softmax_with_cross_entropy(logits,labels)\n",
    "            acc=paddle.metric.accuracy(pred,labels)\n",
    "            accuracies.append(acc.numpy())\n",
    "            losses.append(loss.numpy())\n",
    "            print(\"[validation]accuracy/loss:{}/{}\".format(np.mean(accuracies),np.mean(losses)))\n",
    "            model.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练数据集数量: 50000\n",
      "训练数据集数量: 10000\n",
      "epoch:0,batch:0,loss is:[2.3343134]\n",
      "epoch:0,batch:200,loss is:[0.74498606]\n",
      "epoch:0,batch:400,loss is:[0.31956586]\n",
      "[validation]accuracy/loss:0.8700000047683716/0.3084318935871124\n",
      "[validation]accuracy/loss:0.8949999809265137/0.27609938383102417\n",
      "[validation]accuracy/loss:0.8933332562446594/0.3227061629295349\n",
      "[validation]accuracy/loss:0.8824999332427979/0.3733062446117401\n",
      "[validation]accuracy/loss:0.8819999694824219/0.4069315791130066\n",
      "[validation]accuracy/loss:0.8733332753181458/0.42415741086006165\n",
      "[validation]accuracy/loss:0.8799999952316284/0.4106995165348053\n",
      "[validation]accuracy/loss:0.8812500238418579/0.40680044889450073\n",
      "[validation]accuracy/loss:0.8855555653572083/0.40084540843963623\n",
      "[validation]accuracy/loss:0.8840000033378601/0.39310401678085327\n",
      "[validation]accuracy/loss:0.8863636255264282/0.3845232129096985\n",
      "[validation]accuracy/loss:0.887499988079071/0.3829713463783264\n",
      "[validation]accuracy/loss:0.88692307472229/0.3770497739315033\n",
      "[validation]accuracy/loss:0.8885713815689087/0.3708767294883728\n",
      "[validation]accuracy/loss:0.8913333415985107/0.3599952459335327\n",
      "[validation]accuracy/loss:0.893750011920929/0.35172218084335327\n",
      "[validation]accuracy/loss:0.8947058916091919/0.3470616340637207\n",
      "[validation]accuracy/loss:0.897777795791626/0.3399524390697479\n",
      "[validation]accuracy/loss:0.8989473581314087/0.3343106210231781\n",
      "[validation]accuracy/loss:0.8975000381469727/0.33737146854400635\n",
      "[validation]accuracy/loss:0.8971428871154785/0.33851358294487\n",
      "[validation]accuracy/loss:0.892727255821228/0.3486616611480713\n",
      "[validation]accuracy/loss:0.8899999856948853/0.3522181510925293\n",
      "[validation]accuracy/loss:0.8912500739097595/0.3514479696750641\n",
      "[validation]accuracy/loss:0.8932000994682312/0.3462787866592407\n",
      "[validation]accuracy/loss:0.8957692384719849/0.34028810262680054\n",
      "[validation]accuracy/loss:0.8951851725578308/0.3410940170288086\n",
      "[validation]accuracy/loss:0.8932142853736877/0.34473729133605957\n",
      "[validation]accuracy/loss:0.8927586078643799/0.34809884428977966\n",
      "[validation]accuracy/loss:0.8899999856948853/0.3600669801235199\n",
      "[validation]accuracy/loss:0.8909677267074585/0.3566162884235382\n",
      "[validation]accuracy/loss:0.8921874761581421/0.3539988398551941\n",
      "[validation]accuracy/loss:0.892727255821228/0.35218778252601624\n",
      "[validation]accuracy/loss:0.8941176533699036/0.34722375869750977\n",
      "[validation]accuracy/loss:0.8954285979270935/0.34464871883392334\n",
      "[validation]accuracy/loss:0.895277738571167/0.34584730863571167\n",
      "[validation]accuracy/loss:0.8959459066390991/0.34435468912124634\n",
      "[validation]accuracy/loss:0.8963157534599304/0.3424559235572815\n",
      "[validation]accuracy/loss:0.8951281309127808/0.3433481752872467\n",
      "[validation]accuracy/loss:0.8947499990463257/0.3448057770729065\n",
      "[validation]accuracy/loss:0.895365834236145/0.3436923027038574\n",
      "[validation]accuracy/loss:0.894761860370636/0.3445080816745758\n",
      "[validation]accuracy/loss:0.8960464596748352/0.3412584066390991\n",
      "[validation]accuracy/loss:0.8965908288955688/0.34085288643836975\n",
      "[validation]accuracy/loss:0.8964443802833557/0.3388325273990631\n",
      "[validation]accuracy/loss:0.8958694338798523/0.341538667678833\n",
      "[validation]accuracy/loss:0.8963828682899475/0.33909574151039124\n",
      "[validation]accuracy/loss:0.8960416913032532/0.3382696509361267\n",
      "[validation]accuracy/loss:0.895510196685791/0.33923837542533875\n",
      "[validation]accuracy/loss:0.8942000865936279/0.3425845801830292\n",
      "[validation]accuracy/loss:0.8943138122558594/0.3414730727672577\n",
      "[validation]accuracy/loss:0.8950001001358032/0.3394436538219452\n",
      "[validation]accuracy/loss:0.8956604599952698/0.3366515636444092\n",
      "[validation]accuracy/loss:0.8962963819503784/0.33542999625205994\n",
      "[validation]accuracy/loss:0.8963637351989746/0.3344886898994446\n",
      "[validation]accuracy/loss:0.8969643712043762/0.33222970366477966\n",
      "[validation]accuracy/loss:0.8977193236351013/0.3298240900039673\n",
      "[validation]accuracy/loss:0.8979310393333435/0.32884708046913147\n",
      "[validation]accuracy/loss:0.897796630859375/0.3291773200035095\n",
      "[validation]accuracy/loss:0.8981666564941406/0.3274255692958832\n",
      "[validation]accuracy/loss:0.8988524079322815/0.3260238766670227\n",
      "[validation]accuracy/loss:0.8990322351455688/0.32455000281333923\n",
      "[validation]accuracy/loss:0.8984126448631287/0.324790358543396\n",
      "[validation]accuracy/loss:0.8984375/0.32486334443092346\n",
      "[validation]accuracy/loss:0.898769199848175/0.3253978490829468\n",
      "[validation]accuracy/loss:0.8992424011230469/0.32397618889808655\n",
      "[validation]accuracy/loss:0.898955225944519/0.32463541626930237\n",
      "[validation]accuracy/loss:0.9000000357627869/0.3221993148326874\n",
      "[validation]accuracy/loss:0.9004347920417786/0.3214615285396576\n",
      "[validation]accuracy/loss:0.9011428952217102/0.31935587525367737\n",
      "[validation]accuracy/loss:0.9016901850700378/0.31900855898857117\n",
      "[validation]accuracy/loss:0.902222216129303/0.3173351287841797\n",
      "[validation]accuracy/loss:0.9017808437347412/0.31717365980148315\n",
      "[validation]accuracy/loss:0.9009459018707275/0.3184043765068054\n",
      "[validation]accuracy/loss:0.9007999897003174/0.31807243824005127\n",
      "[validation]accuracy/loss:0.9006578326225281/0.3192332983016968\n",
      "[validation]accuracy/loss:0.8999999165534973/0.3199343681335449\n",
      "[validation]accuracy/loss:0.8996153473854065/0.3216003179550171\n",
      "[validation]accuracy/loss:0.8999999761581421/0.32048487663269043\n",
      "[validation]accuracy/loss:0.8998750448226929/0.3204251825809479\n",
      "[validation]accuracy/loss:0.8995062708854675/0.3201449513435364\n",
      "[validation]accuracy/loss:0.8997561931610107/0.31871145963668823\n",
      "[validation]accuracy/loss:0.9006025195121765/0.31644636392593384\n",
      "[validation]accuracy/loss:0.9011905193328857/0.31512531638145447\n",
      "[validation]accuracy/loss:0.901411771774292/0.31378135085105896\n",
      "[validation]accuracy/loss:0.9017442464828491/0.3122612237930298\n",
      "[validation]accuracy/loss:0.9021839499473572/0.3112821578979492\n",
      "[validation]accuracy/loss:0.9028409719467163/0.3097389340400696\n",
      "[validation]accuracy/loss:0.9033707976341248/0.3082641065120697\n",
      "[validation]accuracy/loss:0.9043333530426025/0.3058490753173828\n",
      "[validation]accuracy/loss:0.9048351049423218/0.30433395504951477\n",
      "[validation]accuracy/loss:0.9055434465408325/0.30233511328697205\n",
      "[validation]accuracy/loss:0.9058064222335815/0.3014098107814789\n",
      "[validation]accuracy/loss:0.9052127003669739/0.3029971122741699\n",
      "[validation]accuracy/loss:0.9049472808837891/0.30332857370376587\n",
      "[validation]accuracy/loss:0.9055209159851074/0.30132633447647095\n",
      "[validation]accuracy/loss:0.905670166015625/0.3006089925765991\n",
      "[validation]accuracy/loss:0.9050000309944153/0.3038744628429413\n",
      "[validation]accuracy/loss:0.90585857629776/0.3016155958175659\n",
      "[validation]accuracy/loss:0.9059000611305237/0.30244243144989014\n",
      "epoch:1,batch:0,loss is:[0.32112303]\n",
      "epoch:1,batch:200,loss is:[0.17700756]\n",
      "epoch:1,batch:400,loss is:[0.2215021]\n",
      "[validation]accuracy/loss:0.9700000286102295/0.14031361043453217\n",
      "[validation]accuracy/loss:0.9600000381469727/0.1505027413368225\n",
      "[validation]accuracy/loss:0.9466667175292969/0.1970149427652359\n",
      "[validation]accuracy/loss:0.9375000596046448/0.23842039704322815\n",
      "[validation]accuracy/loss:0.9280000925064087/0.26492685079574585\n",
      "[validation]accuracy/loss:0.925000011920929/0.28188636898994446\n",
      "[validation]accuracy/loss:0.9300000071525574/0.267667293548584\n",
      "[validation]accuracy/loss:0.9275000095367432/0.26721078157424927\n",
      "[validation]accuracy/loss:0.9266666769981384/0.26559609174728394\n",
      "[validation]accuracy/loss:0.9329999685287476/0.2507965862751007\n",
      "[validation]accuracy/loss:0.9354545474052429/0.2390207201242447\n",
      "[validation]accuracy/loss:0.9366666674613953/0.23323990404605865\n",
      "[validation]accuracy/loss:0.933846116065979/0.23090055584907532\n",
      "[validation]accuracy/loss:0.9357142448425293/0.2251003533601761\n",
      "[validation]accuracy/loss:0.937999963760376/0.2190598100423813\n",
      "[validation]accuracy/loss:0.940000057220459/0.21224907040596008\n",
      "[validation]accuracy/loss:0.940000057220459/0.21151380240917206\n",
      "[validation]accuracy/loss:0.9394444227218628/0.2114654928445816\n",
      "[validation]accuracy/loss:0.9415789246559143/0.2048698365688324\n",
      "[validation]accuracy/loss:0.9409999847412109/0.2071523517370224\n",
      "[validation]accuracy/loss:0.9399999976158142/0.20841245353221893\n",
      "[validation]accuracy/loss:0.9395454525947571/0.2133035510778427\n",
      "[validation]accuracy/loss:0.9382608532905579/0.21606503427028656\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9387500286102295/0.21628563106060028\n",
      "[validation]accuracy/loss:0.9395999908447266/0.21276132762432098\n",
      "[validation]accuracy/loss:0.9407691955566406/0.20737187564373016\n",
      "[validation]accuracy/loss:0.940740704536438/0.20721550285816193\n",
      "[validation]accuracy/loss:0.9396428465843201/0.2081616222858429\n",
      "[validation]accuracy/loss:0.9393103122711182/0.21131418645381927\n",
      "[validation]accuracy/loss:0.9356666803359985/0.2228197455406189\n",
      "[validation]accuracy/loss:0.9354838728904724/0.22096732258796692\n",
      "[validation]accuracy/loss:0.9362499713897705/0.219080850481987\n",
      "[validation]accuracy/loss:0.9372726678848267/0.21755829453468323\n",
      "[validation]accuracy/loss:0.938529372215271/0.21374863386154175\n",
      "[validation]accuracy/loss:0.9391428232192993/0.2119879275560379\n",
      "[validation]accuracy/loss:0.93833327293396/0.21410958468914032\n",
      "[validation]accuracy/loss:0.9381080865859985/0.21459797024726868\n",
      "[validation]accuracy/loss:0.9384210705757141/0.2128651887178421\n",
      "[validation]accuracy/loss:0.9376922845840454/0.21329618990421295\n",
      "[validation]accuracy/loss:0.9370000958442688/0.21454955637454987\n",
      "[validation]accuracy/loss:0.9373171925544739/0.2132728546857834\n",
      "[validation]accuracy/loss:0.937619149684906/0.21206414699554443\n",
      "[validation]accuracy/loss:0.9379070997238159/0.20976892113685608\n",
      "[validation]accuracy/loss:0.9386365413665771/0.2095869928598404\n",
      "[validation]accuracy/loss:0.9388890862464905/0.20864108204841614\n",
      "[validation]accuracy/loss:0.9380436539649963/0.21041835844516754\n",
      "[validation]accuracy/loss:0.9382981061935425/0.20837539434432983\n",
      "[validation]accuracy/loss:0.9379166960716248/0.2075221687555313\n",
      "[validation]accuracy/loss:0.9379591941833496/0.20743981003761292\n",
      "[validation]accuracy/loss:0.9373999834060669/0.20890764892101288\n",
      "[validation]accuracy/loss:0.9372549057006836/0.20752853155136108\n",
      "[validation]accuracy/loss:0.9375/0.2067490518093109\n",
      "[validation]accuracy/loss:0.9377358555793762/0.20556645095348358\n",
      "[validation]accuracy/loss:0.9377777576446533/0.20551325380802155\n",
      "[validation]accuracy/loss:0.9381818175315857/0.2047058790922165\n",
      "[validation]accuracy/loss:0.9385713934898376/0.20301738381385803\n",
      "[validation]accuracy/loss:0.9391227960586548/0.20139609277248383\n",
      "[validation]accuracy/loss:0.939137876033783/0.2020297795534134\n",
      "[validation]accuracy/loss:0.9391524791717529/0.20173461735248566\n",
      "[validation]accuracy/loss:0.9394999146461487/0.20015372335910797\n",
      "[validation]accuracy/loss:0.9395080804824829/0.19929087162017822\n",
      "[validation]accuracy/loss:0.9403225183486938/0.19769546389579773\n",
      "[validation]accuracy/loss:0.9398412108421326/0.19872325658798218\n",
      "[validation]accuracy/loss:0.9396874904632568/0.19874770939350128\n",
      "[validation]accuracy/loss:0.9398461580276489/0.19914190471172333\n",
      "[validation]accuracy/loss:0.9401515126228333/0.1980513483285904\n",
      "[validation]accuracy/loss:0.939701497554779/0.19879521429538727\n",
      "[validation]accuracy/loss:0.9401470422744751/0.19691793620586395\n",
      "[validation]accuracy/loss:0.9405797123908997/0.19591937959194183\n",
      "[validation]accuracy/loss:0.9408571720123291/0.19448667764663696\n",
      "[validation]accuracy/loss:0.9409858584403992/0.19454002380371094\n",
      "[validation]accuracy/loss:0.9415276646614075/0.19298101961612701\n",
      "[validation]accuracy/loss:0.9415068030357361/0.19291667640209198\n",
      "[validation]accuracy/loss:0.9413512945175171/0.19336062669754028\n",
      "[validation]accuracy/loss:0.9415999054908752/0.19253838062286377\n",
      "[validation]accuracy/loss:0.9415789246559143/0.19225828349590302\n",
      "[validation]accuracy/loss:0.9415584206581116/0.19253341853618622\n",
      "[validation]accuracy/loss:0.9408974051475525/0.1939586102962494\n",
      "[validation]accuracy/loss:0.9411392211914062/0.1930871158838272\n",
      "[validation]accuracy/loss:0.9413749575614929/0.19259150326251984\n",
      "[validation]accuracy/loss:0.9412345290184021/0.19258368015289307\n",
      "[validation]accuracy/loss:0.94158536195755/0.19131149351596832\n",
      "[validation]accuracy/loss:0.9422891736030579/0.18952305614948273\n",
      "[validation]accuracy/loss:0.9426190257072449/0.1883338987827301\n",
      "[validation]accuracy/loss:0.9429411888122559/0.1872406154870987\n",
      "[validation]accuracy/loss:0.9431395530700684/0.18633383512496948\n",
      "[validation]accuracy/loss:0.9433333277702332/0.18565978109836578\n",
      "[validation]accuracy/loss:0.9438636302947998/0.18440601229667664\n",
      "[validation]accuracy/loss:0.9439325332641602/0.1845100373029709\n",
      "[validation]accuracy/loss:0.9444443583488464/0.1827671378850937\n",
      "[validation]accuracy/loss:0.9449449181556702/0.181424081325531\n",
      "[validation]accuracy/loss:0.9452173113822937/0.18006885051727295\n",
      "[validation]accuracy/loss:0.9452686905860901/0.17940251529216766\n",
      "[validation]accuracy/loss:0.9449998736381531/0.17986182868480682\n",
      "[validation]accuracy/loss:0.9446314573287964/0.18009603023529053\n",
      "[validation]accuracy/loss:0.9451041221618652/0.17883658409118652\n",
      "[validation]accuracy/loss:0.9454638957977295/0.17831721901893616\n",
      "[validation]accuracy/loss:0.9449999928474426/0.18243445456027985\n",
      "[validation]accuracy/loss:0.945555567741394/0.180826336145401\n",
      "[validation]accuracy/loss:0.945900022983551/0.18161427974700928\n",
      "epoch:2,batch:0,loss is:[0.20251365]\n",
      "epoch:2,batch:200,loss is:[0.15206635]\n",
      "epoch:2,batch:400,loss is:[0.26428172]\n",
      "[validation]accuracy/loss:0.9800000190734863/0.09135347604751587\n",
      "[validation]accuracy/loss:0.9650000333786011/0.10598026216030121\n",
      "[validation]accuracy/loss:0.9566667079925537/0.15930336713790894\n",
      "[validation]accuracy/loss:0.9475000500679016/0.1940680295228958\n",
      "[validation]accuracy/loss:0.940000057220459/0.21619775891304016\n",
      "[validation]accuracy/loss:0.9383333325386047/0.2303253710269928\n",
      "[validation]accuracy/loss:0.9414286017417908/0.22062785923480988\n",
      "[validation]accuracy/loss:0.9387500286102295/0.22276782989501953\n",
      "[validation]accuracy/loss:0.9355555772781372/0.22678348422050476\n",
      "[validation]accuracy/loss:0.9390000104904175/0.21269778907299042\n",
      "[validation]accuracy/loss:0.9427273273468018/0.2012767344713211\n",
      "[validation]accuracy/loss:0.9433333873748779/0.19549348950386047\n",
      "[validation]accuracy/loss:0.9415385127067566/0.1933269053697586\n",
      "[validation]accuracy/loss:0.942142903804779/0.18853938579559326\n",
      "[validation]accuracy/loss:0.9446666836738586/0.1835395097732544\n",
      "[validation]accuracy/loss:0.9456250667572021/0.1767456978559494\n",
      "[validation]accuracy/loss:0.9458824396133423/0.17609117925167084\n",
      "[validation]accuracy/loss:0.946111261844635/0.17668098211288452\n",
      "[validation]accuracy/loss:0.9473685026168823/0.17126043140888214\n",
      "[validation]accuracy/loss:0.9470001459121704/0.1726830154657364\n",
      "[validation]accuracy/loss:0.946190595626831/0.17364005744457245\n",
      "[validation]accuracy/loss:0.9463638067245483/0.1775093972682953\n",
      "[validation]accuracy/loss:0.946087121963501/0.18074892461299896\n",
      "[validation]accuracy/loss:0.9470832943916321/0.1795112043619156\n",
      "[validation]accuracy/loss:0.9479999542236328/0.17659933865070343\n",
      "[validation]accuracy/loss:0.9496153593063354/0.17182978987693787\n",
      "[validation]accuracy/loss:0.9492592215538025/0.17207807302474976\n",
      "[validation]accuracy/loss:0.9492856860160828/0.1708686649799347\n",
      "[validation]accuracy/loss:0.9493103623390198/0.17314305901527405\n",
      "[validation]accuracy/loss:0.9466667175292969/0.18246711790561676\n",
      "[validation]accuracy/loss:0.9480645656585693/0.17958925664424896\n",
      "[validation]accuracy/loss:0.9478124976158142/0.17848628759384155\n",
      "[validation]accuracy/loss:0.9490908980369568/0.17739912867546082\n",
      "[validation]accuracy/loss:0.9497058987617493/0.17447006702423096\n",
      "[validation]accuracy/loss:0.949999988079071/0.1725296527147293\n",
      "[validation]accuracy/loss:0.949722170829773/0.17366449534893036\n",
      "[validation]accuracy/loss:0.9497297406196594/0.17485490441322327\n",
      "[validation]accuracy/loss:0.949999988079071/0.17300191521644592\n",
      "[validation]accuracy/loss:0.9494871497154236/0.17425329983234406\n",
      "[validation]accuracy/loss:0.9487498998641968/0.1749204695224762\n",
      "[validation]accuracy/loss:0.9487804174423218/0.17429330945014954\n",
      "[validation]accuracy/loss:0.9495237469673157/0.17323309183120728\n",
      "[validation]accuracy/loss:0.9502325057983398/0.1709546446800232\n",
      "[validation]accuracy/loss:0.950681746006012/0.17078205943107605\n",
      "[validation]accuracy/loss:0.9506666660308838/0.16998958587646484\n",
      "[validation]accuracy/loss:0.9504347443580627/0.17133226990699768\n",
      "[validation]accuracy/loss:0.9506382346153259/0.16958127915859222\n",
      "[validation]accuracy/loss:0.9508333206176758/0.16843010485172272\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9512245059013367/0.16798211634159088\n",
      "[validation]accuracy/loss:0.9509999752044678/0.16891497373580933\n",
      "[validation]accuracy/loss:0.9511764645576477/0.1675531566143036\n",
      "[validation]accuracy/loss:0.9511538147926331/0.16692575812339783\n",
      "[validation]accuracy/loss:0.9513207077980042/0.16654109954833984\n",
      "[validation]accuracy/loss:0.9512962698936462/0.1665228009223938\n",
      "[validation]accuracy/loss:0.95127272605896/0.16604246199131012\n",
      "[validation]accuracy/loss:0.9516071677207947/0.16472795605659485\n",
      "[validation]accuracy/loss:0.9519298672676086/0.16325880587100983\n",
      "[validation]accuracy/loss:0.951896607875824/0.16391152143478394\n",
      "[validation]accuracy/loss:0.9516949653625488/0.16357168555259705\n",
      "[validation]accuracy/loss:0.9521666765213013/0.1617811620235443\n",
      "[validation]accuracy/loss:0.9521311521530151/0.1614636480808258\n",
      "[validation]accuracy/loss:0.9527419805526733/0.1598995178937912\n",
      "[validation]accuracy/loss:0.9525397419929504/0.16085973381996155\n",
      "[validation]accuracy/loss:0.9521874785423279/0.16074725985527039\n",
      "[validation]accuracy/loss:0.9521538615226746/0.16089774668216705\n",
      "[validation]accuracy/loss:0.9522727131843567/0.1600286066532135\n",
      "[validation]accuracy/loss:0.9519402980804443/0.16027331352233887\n",
      "[validation]accuracy/loss:0.9523530006408691/0.15859608352184296\n",
      "[validation]accuracy/loss:0.9524638056755066/0.1580348163843155\n",
      "[validation]accuracy/loss:0.953000009059906/0.1569080352783203\n",
      "[validation]accuracy/loss:0.9529576897621155/0.15755990147590637\n",
      "[validation]accuracy/loss:0.9534721374511719/0.1561892032623291\n",
      "[validation]accuracy/loss:0.9531506299972534/0.1563975214958191\n",
      "[validation]accuracy/loss:0.9527025818824768/0.1569235771894455\n",
      "[validation]accuracy/loss:0.9529332518577576/0.1562807559967041\n",
      "[validation]accuracy/loss:0.9531578421592712/0.15576322376728058\n",
      "[validation]accuracy/loss:0.9529869556427002/0.15620338916778564\n",
      "[validation]accuracy/loss:0.9523077011108398/0.15805432200431824\n",
      "[validation]accuracy/loss:0.952531635761261/0.15735310316085815\n",
      "[validation]accuracy/loss:0.9526250958442688/0.15681451559066772\n",
      "[validation]accuracy/loss:0.9524692296981812/0.15693333745002747\n",
      "[validation]accuracy/loss:0.9528050422668457/0.15585285425186157\n",
      "[validation]accuracy/loss:0.9533736705780029/0.15428699553012848\n",
      "[validation]accuracy/loss:0.9536906480789185/0.15316906571388245\n",
      "[validation]accuracy/loss:0.9541178345680237/0.15208125114440918\n",
      "[validation]accuracy/loss:0.9543024897575378/0.15133754909038544\n",
      "[validation]accuracy/loss:0.9544829726219177/0.1507166028022766\n",
      "[validation]accuracy/loss:0.9548863768577576/0.14968061447143555\n",
      "[validation]accuracy/loss:0.9548314213752747/0.14967195689678192\n",
      "[validation]accuracy/loss:0.9553332924842834/0.14820730686187744\n",
      "[validation]accuracy/loss:0.9558241367340088/0.14699238538742065\n",
      "[validation]accuracy/loss:0.956195592880249/0.14585265517234802\n",
      "[validation]accuracy/loss:0.9564515948295593/0.14508551359176636\n",
      "[validation]accuracy/loss:0.9562765955924988/0.14507418870925903\n",
      "[validation]accuracy/loss:0.9559999704360962/0.1451740711927414\n",
      "[validation]accuracy/loss:0.956250011920929/0.14429858326911926\n",
      "[validation]accuracy/loss:0.9563918113708496/0.14400404691696167\n",
      "[validation]accuracy/loss:0.955816388130188/0.14816100895404816\n",
      "[validation]accuracy/loss:0.9562627077102661/0.14685989916324615\n",
      "[validation]accuracy/loss:0.9565001130104065/0.1478518694639206\n",
      "epoch:3,batch:0,loss is:[0.17154568]\n",
      "epoch:3,batch:200,loss is:[0.30782828]\n",
      "epoch:3,batch:400,loss is:[0.13675326]\n",
      "[validation]accuracy/loss:0.9700000286102295/0.0749896988272667\n",
      "[validation]accuracy/loss:0.9650000333786011/0.08568793535232544\n",
      "[validation]accuracy/loss:0.9566667079925537/0.1330936700105667\n",
      "[validation]accuracy/loss:0.9475000500679016/0.16825439035892487\n",
      "[validation]accuracy/loss:0.9380000233650208/0.18945373594760895\n",
      "[validation]accuracy/loss:0.9383333325386047/0.19842661917209625\n",
      "[validation]accuracy/loss:0.9414286017417908/0.1871771216392517\n",
      "[validation]accuracy/loss:0.9424999952316284/0.18866890668869019\n",
      "[validation]accuracy/loss:0.9422221779823303/0.18722261488437653\n",
      "[validation]accuracy/loss:0.9439999461174011/0.17977744340896606\n",
      "[validation]accuracy/loss:0.9472727179527283/0.1695196032524109\n",
      "[validation]accuracy/loss:0.9474999904632568/0.1642802357673645\n",
      "[validation]accuracy/loss:0.9461538791656494/0.166158527135849\n",
      "[validation]accuracy/loss:0.9478572010993958/0.1609523892402649\n",
      "[validation]accuracy/loss:0.949999988079071/0.1576574593782425\n",
      "[validation]accuracy/loss:0.9524999856948853/0.15093208849430084\n",
      "[validation]accuracy/loss:0.951764702796936/0.14988701045513153\n",
      "[validation]accuracy/loss:0.9511111378669739/0.1492869108915329\n",
      "[validation]accuracy/loss:0.9515789151191711/0.1458228975534439\n",
      "[validation]accuracy/loss:0.9505000114440918/0.14730913937091827\n",
      "[validation]accuracy/loss:0.9500000476837158/0.14707578718662262\n",
      "[validation]accuracy/loss:0.9495455026626587/0.1518540382385254\n",
      "[validation]accuracy/loss:0.9495652914047241/0.15221281349658966\n",
      "[validation]accuracy/loss:0.949999988079071/0.1500726044178009\n",
      "[validation]accuracy/loss:0.9503999352455139/0.14740000665187836\n",
      "[validation]accuracy/loss:0.9515383839607239/0.14356276392936707\n",
      "[validation]accuracy/loss:0.9518517255783081/0.14394576847553253\n",
      "[validation]accuracy/loss:0.9517856240272522/0.14381363987922668\n",
      "[validation]accuracy/loss:0.951034426689148/0.14704401791095734\n",
      "[validation]accuracy/loss:0.9489999413490295/0.1547423005104065\n",
      "[validation]accuracy/loss:0.9503225088119507/0.15147915482521057\n",
      "[validation]accuracy/loss:0.9509375095367432/0.14991815388202667\n",
      "[validation]accuracy/loss:0.9521211981773376/0.14916035532951355\n",
      "[validation]accuracy/loss:0.9523530006408691/0.14690366387367249\n",
      "[validation]accuracy/loss:0.9528571963310242/0.14479757845401764\n",
      "[validation]accuracy/loss:0.9533334374427795/0.14536187052726746\n",
      "[validation]accuracy/loss:0.9527028203010559/0.14730769395828247\n",
      "[validation]accuracy/loss:0.9531580209732056/0.14616063237190247\n",
      "[validation]accuracy/loss:0.9528205990791321/0.14686526358127594\n",
      "[validation]accuracy/loss:0.9530000686645508/0.14754819869995117\n",
      "[validation]accuracy/loss:0.9534147381782532/0.14683638513088226\n",
      "[validation]accuracy/loss:0.954047679901123/0.14566421508789062\n",
      "[validation]accuracy/loss:0.9546512365341187/0.14384415745735168\n",
      "[validation]accuracy/loss:0.9552273154258728/0.14417491853237152\n",
      "[validation]accuracy/loss:0.9555556178092957/0.14263887703418732\n",
      "[validation]accuracy/loss:0.9554348587989807/0.14393961429595947\n",
      "[validation]accuracy/loss:0.9561703205108643/0.1421581506729126\n",
      "[validation]accuracy/loss:0.9564583897590637/0.1411219984292984\n",
      "[validation]accuracy/loss:0.9563266038894653/0.14120034873485565\n",
      "[validation]accuracy/loss:0.956200122833252/0.1414002925157547\n",
      "[validation]accuracy/loss:0.9564707279205322/0.14060509204864502\n",
      "[validation]accuracy/loss:0.9563462734222412/0.13984715938568115\n",
      "[validation]accuracy/loss:0.9562265872955322/0.13994482159614563\n",
      "[validation]accuracy/loss:0.9564816355705261/0.13942453265190125\n",
      "[validation]accuracy/loss:0.9565455913543701/0.1390552669763565\n",
      "[validation]accuracy/loss:0.956964373588562/0.13812075555324554\n",
      "[validation]accuracy/loss:0.9577193856239319/0.13666804134845734\n",
      "[validation]accuracy/loss:0.9575862884521484/0.13761772215366364\n",
      "[validation]accuracy/loss:0.9574577212333679/0.1373245120048523\n",
      "[validation]accuracy/loss:0.958000123500824/0.13588865101337433\n",
      "[validation]accuracy/loss:0.9581968784332275/0.13554075360298157\n",
      "[validation]accuracy/loss:0.9587098360061646/0.1345565766096115\n",
      "[validation]accuracy/loss:0.9582541584968567/0.13558557629585266\n",
      "[validation]accuracy/loss:0.9582812786102295/0.13525553047657013\n",
      "[validation]accuracy/loss:0.958307683467865/0.1356724053621292\n",
      "[validation]accuracy/loss:0.9584848880767822/0.13473880290985107\n",
      "[validation]accuracy/loss:0.9582090377807617/0.1349223256111145\n",
      "[validation]accuracy/loss:0.958529531955719/0.13388057053089142\n",
      "[validation]accuracy/loss:0.9585508108139038/0.133559912443161\n",
      "[validation]accuracy/loss:0.9588572978973389/0.1324518769979477\n",
      "[validation]accuracy/loss:0.9588733911514282/0.13278023898601532\n",
      "[validation]accuracy/loss:0.9593055248260498/0.13145454227924347\n",
      "[validation]accuracy/loss:0.9594520926475525/0.13111437857151031\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9590540528297424/0.1313418298959732\n",
      "[validation]accuracy/loss:0.9592000246047974/0.13097575306892395\n",
      "[validation]accuracy/loss:0.9593421816825867/0.1308608055114746\n",
      "[validation]accuracy/loss:0.9590910077095032/0.13119612634181976\n",
      "[validation]accuracy/loss:0.9584616422653198/0.13268955051898956\n",
      "[validation]accuracy/loss:0.9587343335151672/0.13203367590904236\n",
      "[validation]accuracy/loss:0.9587500691413879/0.13133350014686584\n",
      "[validation]accuracy/loss:0.9585186243057251/0.13164259493350983\n",
      "[validation]accuracy/loss:0.9586586356163025/0.13094593584537506\n",
      "[validation]accuracy/loss:0.9591567516326904/0.12959755957126617\n",
      "[validation]accuracy/loss:0.9594048857688904/0.12883204221725464\n",
      "[validation]accuracy/loss:0.9595295786857605/0.1281624585390091\n",
      "[validation]accuracy/loss:0.9597676396369934/0.12721726298332214\n",
      "[validation]accuracy/loss:0.9597702622413635/0.126816987991333\n",
      "[validation]accuracy/loss:0.9601137042045593/0.1257495880126953\n",
      "[validation]accuracy/loss:0.9598877429962158/0.12605337798595428\n",
      "[validation]accuracy/loss:0.9603334069252014/0.12479530274868011\n",
      "[validation]accuracy/loss:0.9607692956924438/0.12370216101408005\n",
      "[validation]accuracy/loss:0.9610870480537415/0.12257025390863419\n",
      "[validation]accuracy/loss:0.9611828923225403/0.12204748392105103\n",
      "[validation]accuracy/loss:0.9611702561378479/0.12228493392467499\n",
      "[validation]accuracy/loss:0.961052656173706/0.12235218286514282\n",
      "[validation]accuracy/loss:0.9614583849906921/0.12149748206138611\n",
      "[validation]accuracy/loss:0.961443305015564/0.12147197872400284\n",
      "[validation]accuracy/loss:0.961122453212738/0.12543590366840363\n",
      "[validation]accuracy/loss:0.9614141583442688/0.12439213693141937\n",
      "[validation]accuracy/loss:0.9616000652313232/0.1257692575454712\n",
      "epoch:4,batch:0,loss is:[0.12300509]\n",
      "epoch:4,batch:200,loss is:[0.09412529]\n",
      "epoch:4,batch:400,loss is:[0.11398296]\n",
      "[validation]accuracy/loss:0.9900000095367432/0.06774593144655228\n",
      "[validation]accuracy/loss:0.9750000238418579/0.07504508644342422\n",
      "[validation]accuracy/loss:0.9666666984558105/0.12545247375965118\n",
      "[validation]accuracy/loss:0.9550000429153442/0.15954016149044037\n",
      "[validation]accuracy/loss:0.9480000734329224/0.17790533602237701\n",
      "[validation]accuracy/loss:0.9500000476837158/0.18960155546665192\n",
      "[validation]accuracy/loss:0.949999988079071/0.18094013631343842\n",
      "[validation]accuracy/loss:0.9500000476837158/0.18337522447109222\n",
      "[validation]accuracy/loss:0.9466667175292969/0.18813087046146393\n",
      "[validation]accuracy/loss:0.949999988079071/0.17607812583446503\n",
      "[validation]accuracy/loss:0.9527272582054138/0.16547633707523346\n",
      "[validation]accuracy/loss:0.9524999260902405/0.15929041802883148\n",
      "[validation]accuracy/loss:0.9515383839607239/0.16001513600349426\n",
      "[validation]accuracy/loss:0.9528570771217346/0.15557000041007996\n",
      "[validation]accuracy/loss:0.9546666741371155/0.15130966901779175\n",
      "[validation]accuracy/loss:0.9562500715255737/0.144899383187294\n",
      "[validation]accuracy/loss:0.9564706087112427/0.14524619281291962\n",
      "[validation]accuracy/loss:0.9561111927032471/0.14690908789634705\n",
      "[validation]accuracy/loss:0.9573684334754944/0.14261478185653687\n",
      "[validation]accuracy/loss:0.9565000534057617/0.14400701224803925\n",
      "[validation]accuracy/loss:0.9566666483879089/0.14322985708713531\n",
      "[validation]accuracy/loss:0.9563636779785156/0.14524289965629578\n",
      "[validation]accuracy/loss:0.956087052822113/0.14731371402740479\n",
      "[validation]accuracy/loss:0.9570834040641785/0.14466643333435059\n",
      "[validation]accuracy/loss:0.9575999975204468/0.14232897758483887\n",
      "[validation]accuracy/loss:0.9592307806015015/0.1380070447921753\n",
      "[validation]accuracy/loss:0.9588889479637146/0.13838043808937073\n",
      "[validation]accuracy/loss:0.9592857360839844/0.13660591840744019\n",
      "[validation]accuracy/loss:0.9586207270622253/0.1384289711713791\n",
      "[validation]accuracy/loss:0.9573333859443665/0.14595960080623627\n",
      "[validation]accuracy/loss:0.9580645561218262/0.14355598390102386\n",
      "[validation]accuracy/loss:0.9581250548362732/0.14240816235542297\n",
      "[validation]accuracy/loss:0.9590909481048584/0.14190441370010376\n",
      "[validation]accuracy/loss:0.9591176509857178/0.13989493250846863\n",
      "[validation]accuracy/loss:0.959428608417511/0.13808807730674744\n",
      "[validation]accuracy/loss:0.9583333134651184/0.13945390284061432\n",
      "[validation]accuracy/loss:0.958108127117157/0.14198975265026093\n",
      "[validation]accuracy/loss:0.9581578969955444/0.1404295265674591\n",
      "[validation]accuracy/loss:0.9576922655105591/0.1419583559036255\n",
      "[validation]accuracy/loss:0.9574999809265137/0.14226558804512024\n",
      "[validation]accuracy/loss:0.9575609564781189/0.14165858924388885\n",
      "[validation]accuracy/loss:0.9580951929092407/0.14060375094413757\n",
      "[validation]accuracy/loss:0.9586045742034912/0.13848647475242615\n",
      "[validation]accuracy/loss:0.9586362838745117/0.13820712268352509\n",
      "[validation]accuracy/loss:0.958888828754425/0.1369878351688385\n",
      "[validation]accuracy/loss:0.9586955904960632/0.13815875351428986\n",
      "[validation]accuracy/loss:0.9591488838195801/0.13652294874191284\n",
      "[validation]accuracy/loss:0.9595833420753479/0.13514572381973267\n",
      "[validation]accuracy/loss:0.9593877792358398/0.13482891023159027\n",
      "[validation]accuracy/loss:0.9596000909805298/0.1354355663061142\n",
      "[validation]accuracy/loss:0.9600000381469727/0.1340908408164978\n",
      "[validation]accuracy/loss:0.9600000381469727/0.13354799151420593\n",
      "[validation]accuracy/loss:0.9600000381469727/0.13360969722270966\n",
      "[validation]accuracy/loss:0.9599999785423279/0.13341370224952698\n",
      "[validation]accuracy/loss:0.9599999785423279/0.13296113908290863\n",
      "[validation]accuracy/loss:0.960178554058075/0.13231028616428375\n",
      "[validation]accuracy/loss:0.9605262875556946/0.13107319176197052\n",
      "[validation]accuracy/loss:0.9605172276496887/0.13220542669296265\n",
      "[validation]accuracy/loss:0.9605084657669067/0.13188359141349792\n",
      "[validation]accuracy/loss:0.9610000252723694/0.1302269697189331\n",
      "[validation]accuracy/loss:0.9609835743904114/0.13016565144062042\n",
      "[validation]accuracy/loss:0.96161288022995/0.12880882620811462\n",
      "[validation]accuracy/loss:0.9612697958946228/0.13010019063949585\n",
      "[validation]accuracy/loss:0.9612500071525574/0.12979766726493835\n",
      "[validation]accuracy/loss:0.961384654045105/0.12980879843235016\n",
      "[validation]accuracy/loss:0.9615151882171631/0.12909381091594696\n",
      "[validation]accuracy/loss:0.9613433480262756/0.12883926928043365\n",
      "[validation]accuracy/loss:0.9617647528648376/0.1274958997964859\n",
      "[validation]accuracy/loss:0.961884081363678/0.12725260853767395\n",
      "[validation]accuracy/loss:0.9621429443359375/0.1261224001646042\n",
      "[validation]accuracy/loss:0.9619718790054321/0.12673087418079376\n",
      "[validation]accuracy/loss:0.9623610973358154/0.12551447749137878\n",
      "[validation]accuracy/loss:0.9621917605400085/0.12574733793735504\n",
      "[validation]accuracy/loss:0.9620269536972046/0.12609198689460754\n",
      "[validation]accuracy/loss:0.961866557598114/0.1258201003074646\n",
      "[validation]accuracy/loss:0.9619736075401306/0.12513849139213562\n",
      "[validation]accuracy/loss:0.9618180394172668/0.12542350590229034\n",
      "[validation]accuracy/loss:0.9612818956375122/0.12692664563655853\n",
      "[validation]accuracy/loss:0.9613922834396362/0.12634126842021942\n",
      "[validation]accuracy/loss:0.9614999890327454/0.12578244507312775\n",
      "[validation]accuracy/loss:0.9614814519882202/0.126016303896904\n",
      "[validation]accuracy/loss:0.961707353591919/0.12521396577358246\n",
      "[validation]accuracy/loss:0.9621686935424805/0.12386520206928253\n",
      "[validation]accuracy/loss:0.9624999761581421/0.1229456439614296\n",
      "[validation]accuracy/loss:0.9628235101699829/0.12201420217752457\n",
      "[validation]accuracy/loss:0.9629069566726685/0.12120039016008377\n",
      "[validation]accuracy/loss:0.9631034731864929/0.12075488269329071\n",
      "[validation]accuracy/loss:0.9634090662002563/0.11984819173812866\n",
      "[validation]accuracy/loss:0.9632583856582642/0.12023328989744186\n",
      "[validation]accuracy/loss:0.9635554552078247/0.11912083625793457\n",
      "[validation]accuracy/loss:0.9639559984207153/0.11808477342128754\n",
      "[validation]accuracy/loss:0.9642390608787537/0.11710821837186813\n",
      "[validation]accuracy/loss:0.9645160436630249/0.11638451367616653\n",
      "[validation]accuracy/loss:0.9643615484237671/0.11649875342845917\n",
      "[validation]accuracy/loss:0.9644209146499634/0.11642760783433914\n",
      "[validation]accuracy/loss:0.9645832180976868/0.11600831151008606\n",
      "[validation]accuracy/loss:0.9646390676498413/0.11584911495447159\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9641835689544678/0.12029381841421127\n",
      "[validation]accuracy/loss:0.9645453691482544/0.11924544721841812\n",
      "[validation]accuracy/loss:0.964699923992157/0.12052536755800247\n"
     ]
    }
   ],
   "source": [
    "train_loader=load_data('train')\n",
    "valid_loader=load_data('valid')\n",
    "model=LeNetModel()\n",
    "train(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "paddle.save(model.state_dict(),'mnist-cnn.pdparams')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "im=Image.open('9.jpg').convert('L')\n",
    "im=im.resize((28,28),Image.ANTIALIAS)\n",
    "img=np.array(im).reshape(1,1,28,28).astype('float32')\n",
    "img=1.0-img/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/plain": [
       "<Figure size 144x144 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.figure(figsize=(2,2))\n",
    "plt.imshow(im,cmap=plt.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本次预测的数字是: 9\n"
     ]
    }
   ],
   "source": [
    "model=LeNetModel()\n",
    "params_file_path='mnist-cnn.pdparams'\n",
    "param_dict=paddle.load(params_file_path)\n",
    "model.load_dict(param_dict)\n",
    "model.eval()\n",
    "tensor_img=img\n",
    "results=model(paddle.to_tensor(tensor_img))\n",
    "lab=np.argsort(results.numpy())\n",
    "print(\"本次预测的数字是:\",lab[0][-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
